/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package check;

import Main.ExpoManagerMain;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 *
 * @author Mai Lan
 */
public class check {
     public check() {
    }
    /**
     * Method to check Input-Name
     * @return boolean true or false
     * @param n
     */
    public static boolean checkName(String n) {

        if (n == null || n.length() == 0) {
            return false;
        } else {
            String strPattern = "[a-zA-Z]\\s";

            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return m.find();
        }
    }
  
    
    /**
     * Method to check Email
     * @return boolean true or false
     * @param n
     */
    public static boolean checkEmail(String n) {
        if (n == null) {
            return false;
        } else {
            String strPattern = "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b";
            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return m.find();
        }
    }
    public static boolean  checkday(String Birth){
        Pattern patBirth=Pattern.compile("\\d{2}/\\d{2}/\\d{4}");
        Matcher mathBirth=patBirth.matcher(Birth);
        return mathBirth.matches();
    }
  
    /**
     * Method to check Phone
     * @return boolean true or false
     * @param n
     */
    public static boolean checkPhone(String n) {
       if (n == null || n.length() > 11  || n.length() < 8) {
            return false;
        } else {
            String strPattern = "[0-9]";
            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return m.find();
        }
    }
        /**
     * Method checkNumber(), to check input number.
     * @return boolean true or false.
     * @param n
     */
    public static boolean checkNumber(String n) {
        if (n == null || n.length() > 10  || n.length() < 1) {
            return false;
        } else {
            String strPattern = "[^0-9]";
            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return m.find();
        }
    }
    public static  boolean  checkDay(JTextField day){
        check c=new check();
            if(day.getText().equals("")){
              JOptionPane.showMessageDialog(null, "Enter Day create of product");
              day.requestFocus();
             return false;
        }else{
              if(!checkday(day.getText().trim())){

                      JOptionPane.showMessageDialog(null, "Formart day is dd/mm/yyyy");
                      day.requestFocus();
                     return false;
              }else {
                String str[]=day.getText().split("/");
                if(Integer.parseInt(str[0])>31){
                    JOptionPane.showMessageDialog(day, "Day less than 31");
                    day.requestFocus();
                    return false;
                    
                }else{
                    if(  Integer.parseInt(str[1])>12){
                        JOptionPane.showMessageDialog(day, " mouth <=12 ");
                        day.requestFocus();
                       return false;
                    }else {
                        if(Integer.parseInt(str[1])==2){
                            if(Integer.parseInt(str[0])>29){
                                JOptionPane.showMessageDialog(day, "Febuary have 28 or 29 days");
                                 day.requestFocus();
                                 
                                return false;
                            }else{
                                if(Integer.parseInt(str[2])%4!=0){
                                   if(Integer.parseInt(str[0])>=29){
                                    JOptionPane.showMessageDialog(day, "Febuary of  "+str[2]+" have 28 days");
                                     day.requestFocus();
                                   return false;
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }
        return true;

    }
    public static boolean  checkReadlyData(String tName,String fiel,JTextField data,Connection conn){
        try {
            String sql = "select * from " + tName;
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                if(data.getText().equals(rs.getString(fiel))){
                      JOptionPane.showMessageDialog(null, data.getText()+"is ready!" );
                      data.requestFocus();
                      return false;
                }
            }
            
            
        } catch (SQLException ex) {
            Logger.getLogger(check.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
     public static  void id(JComboBox cob,String tableName)throws SQLException{
        try {
            String sqlIdEx = "select ID from "+tableName;
            Statement st = ExpoManagerMain.conn.createStatement();
            ResultSet rs = st.executeQuery(sqlIdEx);
            while (rs.next()) {
                cob.addItem(rs.getString("ID"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(check.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     public static  void ValuesFiel(JComboBox cob,String tableName,String colum)throws SQLException{
        try {
            String sqlIdEx = "select "+colum+" from "+tableName;
            Statement st = ExpoManagerMain.conn.createStatement();
            ResultSet rs = st.executeQuery(sqlIdEx);
            while (rs.next()) {
                cob.addItem(rs.getString(colum));
            }
        } catch (SQLException ex) {
            Logger.getLogger(check.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static  void ValuesFiel(JComboBox cob,String tableName,String colum,String colum2)throws SQLException{
        try {
            String sqlIdEx = "select * from "+tableName;
            Statement st = ExpoManagerMain.conn.createStatement();
            ResultSet rs = st.executeQuery(sqlIdEx);
            while (rs.next()) {
                cob.addItem(rs.getString(colum)+" -- "+colum2+" :"+rs.getString(colum2));
            }
        } catch (SQLException ex) {
            Logger.getLogger(check.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    
   
}
